package com.android.monee.view.model;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;

import com.android.monee.binding.Field;
import com.android.monee.binding.observable.Observable;
import com.android.monee.binding.observable.ObservableImpl;
import com.android.monee.binding.observer.Observer;
import com.android.monee.data.model.Budget;
import com.android.monee.domain.BudgetService;
import com.android.monee.R;

public class BudgetDetails extends BudgetService implements Observable<BudgetDetails>{

	private Resources res;
	private String currency;
	private SimpleDateFormat sdf;
	private Observable<BudgetDetails> observableDelegate;
	
	public BudgetDetails(Budget budget, Resources res) {
		super(budget);
		this.res = res;
		this.observableDelegate = new ObservableImpl<BudgetDetails>(this);
		this.currency = res.getText(R.string.currency).toString();
		this.sdf = new SimpleDateFormat("dd/MM/yyyy");
	}
	
	public Drawable getStateIcon(){
		if (super.getState() == BudgetState.RUNNING)
			return this.res.getDrawable(android.R.drawable.ic_media_play);
		else if (super.getState() == BudgetState.PAUSED)
			return this.res.getDrawable(android.R.drawable.ic_media_pause);
		else
			return null;
	}
	
	public String getTotalAmountLib(){
		return Float.valueOf(super.getTotalAmount()).intValue()+this.currency;
	}
	
	public String getProvAmountLib(){
		return Float.valueOf(super.getProvAmount()).intValue()+this.currency;
	}
	
	public String getBeginLib(){
		return sdf.format(super.getBegin());
	}
	
	public String getTermLib(){
		return sdf.format(super.getTerm());
	}
	
	public String getNbMensLib(){
		return super.getTotalNbMens()+"";
	}
	
	public String getMensAmountLib(){
		return Float.valueOf(super.getMensAmount()).intValue()+this.currency;
	}
	
	public float getNotProvAmount(){
		return super.getTotalAmount()-super.getProvAmount();
	}

	@Override
	public void setState(BudgetState state) {
		super.setState(state);
		this.observableDelegate.notify(StateIcon.class);
	}

	public <T, V> void addObserver(Observer<BudgetDetails, T, V> o) {
		observableDelegate.addObserver(o);
	}

	public <T, V> void deleteObserver(Observer<BudgetDetails, T, V> o) {
		observableDelegate.deleteObserver(o);
	}

	public <T, V> void notify(Class<? extends Field> fieldClass) {
		observableDelegate.notify(fieldClass);
	}

	public BudgetDetails getObservedData() {
		return observableDelegate.getObservedData();
	}
	
	public static List<BudgetDetails> newCollection(Collection<? extends Budget> budgets, Resources res){
		List<BudgetDetails> newCol = new ArrayList<BudgetDetails>();
		for(Budget budget : budgets){
			newCol.add(new BudgetDetails(budget, res));
		}
		return newCol;
	}
	//TODO : voir si on peut modifier Field ou avoir des sous-classes pour des field juste RO
	public class StateIcon implements Field<BudgetDetails, Drawable>{

		@Override
		public Drawable get(BudgetDetails obj) {
			return obj.getStateIcon();
		}

		@Override
		public void set(BudgetDetails obj, Drawable val) {
		}
		
	}
	
//Commenté car l'édition d'unbudget sera fera surement par un fragment + view modèle dédié (contenant notament de la validation comme NewBudget)
//	public class TotalAmountLib implements Field<BudgetDetails, String>{
//
//		@Override
//		public String get(BudgetDetails obj) {
//			return obj.getTotalAmountLib();
//		}
//
//		@Override
//		public void set(BudgetDetails obj, String val) {
//		}
//		
//	}
//	
//	public class ProvAmountLib implements Field<BudgetDetails, String>{
//
//		@Override
//		public String get(BudgetDetails obj) {
//			return obj.getProvAmountLib();
//		}
//
//		@Override
//		public void set(BudgetDetails obj, String val) {
//		}
//		
//	}
//	
//	public class BeginLib implements Field<BudgetDetails, String>{
//
//		@Override
//		public String get(BudgetDetails obj) {
//			return obj.getBeginLib();
//		}
//
//		@Override
//		public void set(BudgetDetails obj, String val) {
//		}
//		
//	}
//	
//	public class TermLib implements Field<BudgetDetails, String>{
//
//		@Override
//		public String get(BudgetDetails obj) {
//			return obj.getTermLib();
//		}
//
//		@Override
//		public void set(BudgetDetails obj, String val) {
//		}
//		
//	}
//	
//	public class NbMensLib implements Field<BudgetDetails, String>{
//
//		@Override
//		public String get(BudgetDetails obj) {
//			return obj.getNbMensLib();
//		}
//
//		@Override
//		public void set(BudgetDetails obj, String val) {
//		}
//		
//	}
//	
//	public class MensAmountLib implements Field<BudgetDetails, String>{
//
//		@Override
//		public String get(BudgetDetails obj) {
//			return obj.getMensAmountLib();
//		}
//
//		@Override
//		public void set(BudgetDetails obj, String val) {
//		}
//		
//	}
//	
//	public class NotProvAmount implements Field<BudgetDetails, Float>{
//
//		@Override
//		public Float get(BudgetDetails obj) {
//			return obj.getNotProvAmount();
//		}
//
//		@Override
//		public void set(BudgetDetails obj, Float val) {
//		}
//		
//	}
}
